package com.teaphy.coroutinecrawler

import kotlinx.coroutines.*


fun main(): Unit = runBlocking {

    println("current thread name: ${Thread.currentThread().name}")

    // 如果不指定Dispatcher,将默认使用父协程的CoroutineDispatcher
//    launch { // context of the parent, main runBlocking coroutine
//        println("main runBlocking : I'm working in thread ${Thread.currentThread().name}")
//    }

//    launch(Dispatchers.Unconfined) {
//
//        // 在线程中执行
//        println("Unconfined : I'm working in thread ${Thread.currentThread().name}")
//
//        delay(1000)
//
//        // 在线程 kotlinx.coroutines.DefaultExecutor 中执行
//        println("Unconfined : I'm working in thread ${Thread.currentThread().name}")
//    }

//    launch(Dispatchers.Default) { // will get dispatched to DefaultDispatcher
//        println("Default : I'm working in thread ${Thread.currentThread().name}")
//        delay(1000)
//        println("Default : I'm working in thread ${Thread.currentThread().name}")
//    }

    launch(Dispatchers.IO) {
        println("IO1 : I'm working in thread ${Thread.currentThread().name}")
        delay(1000)
    }

//    launch(Dispatchers.IO) {
//        println("IO2 : I'm working in thread ${Thread.currentThread().name}")
//        delay(10000)
//    }
//
//    launch(Dispatchers.IO) {
//        println("IO3 : I'm working in thread ${Thread.currentThread().name}")
//        delay(10000)
//    }

    launch(Dispatchers.Main) { // will get dispatched to DefaultDispatcher
        println("Main : I'm working in thread ${Thread.currentThread().name}")
    }

//    launch(newSingleThreadContext("MyOwnThread")) { // will get its own new thread
//        println("newSingleThreadContext: I'm working in thread ${Thread.currentThread().name}")
//    }
}